/*
 * Copyright 2023 devaper
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pigeon

import io.github.devaper.pigeon.annotation.ah.*
import io.github.devaper.pigeon.annotation.request.*
import io.github.devaper.pigeon.response.*
import io.github.devaper.pigeon.support.component.*
import org.junit.jupiter.api.*

class PigeonTest {

    interface TranslateClient {

        @GET("/api/{path}/index.php")
        fun translate(
            @Path("path") path: String = "api-fanyi-yd",
            @Query("msg") msg: String,
            @Query("type") type: Int = 1
        ): Deferred<Map<*, *>>

    }

    class TranslateClientFallback : TranslateClient {
        override fun translate(path: String, msg: String, type: Int): Deferred<Map<*, *>> {
            return Deferred.of(mapOf(
                "from" to "fallback"
            ))
        }
    }

    @Test
    fun `test pigeon`() {
        /*
            fallback要实现Client接口, 无法所有Client共用, 解决办法:
                1. 每次createClient之前设置好自己的fallback
                2. 每次createClient都创建一个新的Pigeon
                3. 每次createClient都传入一个ClientConfig(现在用的是一个重载方法)
         */

        val pigeon = io.github.devaper.pigeon.Pigeon()
            .basePath("https://v.api.aa1.cn")
            .httpClientEngine(GrizzlyHttpClientEngine())
            .httpMessageConverter(JacksonHttpMessageConverter())
            .fallback(TranslateClientFallback())

        val translateClient = pigeon.createClient(TranslateClient::class.java)
        println(translateClient.translate(msg = "你好").get())

    }

}